<?php

declare(strict_types=1);

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

// | Copyright (c) 2021~2024 https://www.lanyingkeji.cnAll rights reserved.
// +----------------------------------------------------------------------
// | Licensed 蘭影科技是一家专业的软件技术服务商
// +----------------------------------------------------------------------
// | Author:  Lanyingkeji
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\agent;

use app\adminapi\controller\AuthController;
use app\services\agent\AgentLevelTaskServices;
use think\facade\App;

/**
 * 分销等级任务控制器
 * Class AgentLevelTask.
 */
class AgentLevelTask extends AuthController
{
    /**
     * AgentLevelTask constructor.
     */
    public function __construct(App $app, AgentLevelTaskServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示等级任务列表.
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['id', 0],
            ['status', ''],
            ['keyword', ''],
        ]);
        if (!$where['id']) {
            return app('json')->fail(100100);
        }
        $where['level_id'] = $where['id'];
        unset($where['id']);

        return app('json')->success($this->services->getLevelTaskList($where));
    }

    /**
     * 等级任务添加表单.
     *
     * @throws \FormBuilder\Exception\FormBuilderException
     *
     * @return mixed
     */
    public function create()
    {
        [$level_id] = $this->request->postMore([
            ['level_id', 0], ], true);
        if (!$level_id) {
            return app('json')->fail(100100);
        }

        return app('json')->success($this->services->createForm((int) $level_id));
    }

    /**
     * 保存等级任务
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @return mixed
     */
    public function save()
    {
        $data = $this->request->postMore([
            ['level_id', 0],
            ['name', ''],
            ['type', ''],
            ['number', 0],
            ['desc', 0],
            ['sort', 0],
            ['status', 0], ]);
        if (!$data['level_id']) {
            return app('json')->fail(100100);
        }
        if (!$data['name']) {
            return app('json')->fail(400207);
        }
        if (!$data['type']) {
            return app('json')->fail(400208);
        }
        if (!$data['number']) {
            return app('json')->fail(400209);
        }
        $this->services->checkTypeTask(0, $data);
        $data['add_time'] = time();
        $this->services->save($data);

        return app('json')->success(400210);
    }

    /**
     * 显示指定的资源.
     *
     * @param $id
     */
    public function read($id): void
    {
    }

    /**
     * 等级任务修改表单.
     *
     * @param $id
     *
     * @throws \FormBuilder\Exception\FormBuilderException
     *
     * @return mixed
     */
    public function edit($id)
    {
        return app('json')->success($this->services->editForm((int) $id));
    }

    /**
     * 修改等级任务
     *
     * @param $id
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @return mixed
     */
    public function update($id)
    {
        $data = $this->request->postMore([
            ['name', ''],
            ['type', ''],
            ['number', 0],
            ['desc', 0],
            ['sort', 0],
            ['status', 0], ]);
        if (!$data['name']) {
            return app('json')->fail(400207);
        }
        if (!$data['type']) {
            return app('json')->fail(400208);
        }
        if (!$data['number']) {
            return app('json')->fail(400209);
        }
        if (!$levelTaskInfo = $this->services->getLevelTaskInfo((int) $id)) {
            return app('json')->fail(400211);
        }
        $this->services->checkTypeTask((int) $id, $data);
        $levelTaskInfo->name = $data['name'];
        $levelTaskInfo->type = $data['type'];
        $levelTaskInfo->number = $data['number'];
        $levelTaskInfo->desc = $data['desc'];
        $levelTaskInfo->sort = $data['sort'];
        $levelTaskInfo->status = $data['status'];
        $levelTaskInfo->save();

        return app('json')->success(100001);
    }

    /**
     * 删除等级任务
     *
     * @param $id
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) {
            return app('json')->fail(100100);
        }
        $levelTaskInfo = $this->services->getLevelTaskInfo((int) $id);
        if ($levelTaskInfo) {
            $res = $this->services->update($id, ['is_del' => 1]);
            if (!$res) {
                return app('json')->fail(100008);
            }
        }

        return app('json')->success(100002);
    }

    /**
     * 修改状态
     *
     * @param int    $id
     * @param string $status
     *
     * @return mixed
     */
    public function set_status($id = 0, $status = '')
    {
        if ('' === $status || 0 === $id) {
            return app('json')->fail(100100);
        }
        $this->services->update($id, ['status' => $status]);

        return app('json')->success(100014);
    }
}
